package com.shuaiche.yeguan.ui.select;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.util.ResourceUtils;
import com.shuaiche.yeguan.views.brandselectbar.ItemBean;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by baoyinbo on 2017/12/6.
 */

public class SCPurchaserSelectAdapter extends RecyclerView.Adapter<SCPurchaserSelectAdapter.ViewHolder> {

    private Context mContext;
    private List<ItemBean> mDatas;
    private LayoutInflater mInflater;

    public SCPurchaserSelectAdapter(Context mContext, List<ItemBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.sc_item_select_purshacer_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final ItemBean itemBean = mDatas.get(position);
        holder.tvName.setText(itemBean.getName());
        if (itemBean.isSelect()) {
            holder.tvName.setTextColor(ResourceUtils.getColor(mContext, R.color.text_yellow));
            holder.ivIsSelect.setVisibility(View.VISIBLE);
        } else {
            holder.tvName.setTextColor(ResourceUtils.getColor(mContext, R.color.text_black));
            holder.ivIsSelect.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callListener.itemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        ImageView ivIsSelect;
        public ViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            ivIsSelect = (ImageView) itemView.findViewById(R.id.ivIsSelect);
        }
    }


    public interface CallbackListener{
        void itemClick(int position);
    }

    /**回调 拨打电话 / 隐藏 监听*/
    public CallbackListener callListener;

    public void setCallbackListener (CallbackListener callListener) {
        this.callListener = callListener;
    }
}
